package com.li.client.util;

import java.util.Stack;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ImageStyle;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * class to handle masking of the Gui when a back end request is progress.
 */
public class BusyMask
{
	public static final BusyMask INSTANCE = new BusyMask();
	private final Stack<String>	actions	= new Stack<String>();
	private final Canvas mask = createModalMessage();
	private Label messageLabel;

	private static final String SKIN = "[SKIN]/";
	
	
	public void show(String actionName)
	{
		if (actionName == null)
		{
			actionName = "";
		}
		actions.push(actionName);
		setMessage();
		mask.show();
	}

	private void setMessage()
	{
		String msg = "Loading Data.";
		for (String action : actions)
		{
			if (!action.equals(""))
			{
				msg = action;
				break;
			}
		}
		msg = msg + " Please wait...";
		messageLabel.setContents(msg);
	}

	public void hide()
	{
		if (!actions.isEmpty())
		{
			actions.pop();
			if (actions.isEmpty())
			{
				mask.hide();
			}
		}
	}

	
	public void hideFinal()
	{
		actions.removeAllElements();
		mask.hide();
	}
	
	public static Img getIcon(String source)
	{
		final Img image = new Img(SKIN + source, AppStyle.px16, AppStyle.px16);
		image.setShowEdges(false);
		image.setImageType(ImageStyle.NORMAL);
		return image;
	}

	private Canvas createModalMessage()
	{
		Img loadingIcon = getIcon("icon_loading.gif");
		messageLabel = new Label();
		messageLabel.setWidth(AppStyle.px200);
		messageLabel.setHeight100();
		messageLabel.setValign(VerticalAlignment.CENTER);
		HLayout hLayout = new HLayout();
		hLayout.setLayoutMargin(AppStyle.px20);
		hLayout.setMembersMargin(AppStyle.px5);
		// hLayout.setBorder(AppStyle.BORDER_1px);
		VLayout vLayout = new VLayout();
		vLayout.setMembers(new LayoutSpacer(), loadingIcon, new LayoutSpacer());
		hLayout.setMembers(new LayoutSpacer(), vLayout, messageLabel, new LayoutSpacer());
		Window window = new Window();
		window.setShowHeader(false);
		window.setShowHeaderBackground(false);
		window.setShowHeaderIcon(false);
		window.setIsModal(true);
//		window.setShowModalMask(GwtUtils.isBusyMaskFadeBackGroud());
		window.setWidth(AppStyle.px300);
		window.setHeight(AppStyle.px120);
		// window.setShowMinimizeButton(false);
		// window.setShowMaximizeButton(false);
		// window.setShowCloseButton(false);
		window.addItem(hLayout);
		window.centerInPage();
		// window.setStyleName("icgLoadingWindow");
		return window;
	}

	private Canvas createModalMessage2()
	{
		// Window win = new Window();
		// win.setWidth(350);
		// win.setHeight(50);
		// win.setShowHeader(false);
		// return win;
		HLayout parent = new HLayout();
		parent.setDefaultLayoutAlign(VerticalAlignment.CENTER);
		parent.hide();
		parent.setHeight100();
		parent.setWidth100();
		Canvas transparent = new Canvas();
		transparent.setWidth100();
		transparent.setHeight100();
		transparent.setBackgroundColor("#555");
		transparent.setOpacity(30);
		parent.addChild(transparent);
		VLayout modal = new VLayout();
		modal.setDefaultLayoutAlign(Alignment.CENTER);
		modal.setHeight(25);
		modal.setZIndex(transparent.getZIndex() + 2);
		parent.addMember(modal);
		return parent;
	}
	
	
}
